import requests
import json
import base64
import re
import execjs
import os
import subprocess
_dir = os.path.dirname(os.path.abspath(__file__))


headers = {
    "accept": "*/*^",
    "accept-language": "zh-CN,zh;q=0.9,en;q=0.8,en-GB;q=0.7,en-US;q=0.6^",
    "authorization": "Bearer mmYcluUktt5aHD7yBS4WAoEuF6qsFRGCybYzxx26wN3jdvJCjO4PYzkpbZ5PdSeQ",
    "content-type": "application/json",
    "origin": "https://chat.deepseek.com",
    "referer": "https://chat.deepseek.com/a/chat/s/67d9fd99-20d4-4ea3-a6b7-94c8044d34ef",
    "user-agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/134.0.0.0 Safari/537.36 Edg/134.0.0.0",
    "cookie": "Hm_lvt_fb5acee01d9182aabb2b61eb816d24ff=1739245353; smidV2=202502111142347527453a612b6a8e409313c2fa30b2c3009f5a7f87ae025b0; .thumbcache_6b2e5483f9d858d7c661c5e276b6a6ae=Ln8ILVHccVLX7sJnnAmnTAEEPJMo/0HNBvvqmD9Cp1KjSxuIotHgYi/kAA+IzB46oLCdfUVWWRqIHulp3SbK+w%3D%3D; intercom-device-id-guh50jw4=0af4d107-6342-4e5a-a78d-f1df7b24f89a; HWWAFSESID=3e970226bdf219b9d35; HWWAFSESTIME=1739502978707; ds_session_id=1a6f91dd285b4e7faa5c83be0d1f2d97",
    "x-app-version": "20241129.1",
    "x-client-locale": "zh_CN",
    "x-client-platform": "web",
    "x-client-version": "1.0.0-always",
    #"x-ds-pow-response": "eyJhbGdvcml0aG0iOiJEZWVwU2Vla0hhc2hWMSIsImNoYWxsZW5nZSI6Ijg1MjM2YWY5MThlZjM1ZjUwOWRlMjdhNzk2NjJkNGU3YjdlNzJhNzU1NTllYjIxYzI4NjZkNWM3ZmI4NzYyNmIiLCJzYWx0IjoiZGI0YzBjMGQ4MjMzZTFiMzMwOWQiLCJhbnN3ZXIiOjk3MDg1LCJzaWduYXR1cmUiOiI0ZGQzOTk1ZTkzMjU2MzIyMzUxNWQzYjczYjNmYjFkNWI3NGE4MGQ4ZGMxY2MwMzdjMDljZGYzMzUyMzhmMzY4IiwidGFyZ2V0X3BhdGgiOiIvYXBpL3YwL2NoYXQvY29tcGxldGlvbiJ9"
}

# url1 = "https://chat.deepseek.com/api/v0/chat/create_pow_challenge"
# data = {
#     "target_path": "/api/v0/chat/completion" 
# }
# json_str = json.dumps(data, indent=4, ensure_ascii=False)


# response = requests.post(url1, headers=headers, data=json_str)

# print(response.text)
# print(response)

# challenge = response.json()["data"]["biz_data"]['challenge']
# print(challenge)

header_respones = {
    "algorithm": "DeepSeekHashV1",
    "challenge": "0cfe01be5a97b2f64b569564345467b044a265005d0d915245d942c80644a281",
    "salt": "f1a721cd01ade02079c7",
    "answer": 86660,
    "signature": "d728ac2d91379a6cf9fe1e5741bb937d19e2592c8f17fb491c154f1f9b93939c",
    "target_path":"/api/v0/chat/completion"
}

# header_respones = {
#     "algorithm": challenge['algorithm'],
#     "challenge": challenge['challenge'],
#     "salt": challenge['salt'],
#     "answer": 78666,
#     "signature": challenge['signature'],
#     "target_path": "/api/v0/chat/completion"
# }

# header_respones_str = json.dumps(header_respones, indent=None, ensure_ascii=False, separators=(",", ":"))
# print(header_respones_str)
# header_respones_base = base64.b64encode(header_respones_str.encode('utf-8'))
# print(header_respones_base)
# headers['x-ds-pow-response'] = header_respones_base

# url = "https://chat.deepseek.com/api/v0/chat/completion"
# data = {
#     "chat_session_id": "3015c114-4c03-4185-916a-fd9a6f2149b2",
#     "parent_message_id": 59,
#     "prompt": "你是谁",
#     "ref_file_ids": [],
#     "search_enabled": False,
#     "thinking_enabled": False,
# }
# json_str = json.dumps(data, indent=4, ensure_ascii=True)
# response = requests.post(url, headers=headers, data=json_str, stream=True)

# result = ""
# pattern = r'"content":"(.*?)"'
# for chunk in response.iter_content(chunk_size=1024):
#     # 处理响应内容
#     data = chunk.decode('utf-8')
#     print(data)
#     matches = re.findall(pattern, data)
#     if matches:
#         result += matches[0]
# print(result)
# print(response.text)
# print(response)

js = execjs.compile(open(os.path.join(_dir, 'deepseek.js'), encoding='utf-8').read())

print(js.call('loadWasm',{
        "algorithm": "DeepSeekHashV1",
        "challenge": "5be4be9f6e2d7189907bf226381f735ee9b6e782ee1f713b501fe07d2dbd0626",
        "salt": "85df87f830baf6dd15a0",
        "signature": "e99b499296e2595ac46fc6bc38485844d2f774ca2fab9b47405547a7493d7c72",
        "difficulty": 144000,
        "expire_at": 1742461733715,
        "expire_after": 300000,
        "target_path": "/api/v0/chat/completion"
    }, os.path.join(_dir, 'sha3_wasm_bg.7b9ca65ddd.wasm')))